package com.pactera.chatRoom.controller;

import java.util.List;

import com.pactera.chatRoom.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.pactera.chatRoom.entity.Person;
import com.pactera.chatRoom.service.PersonServcie;

@RestController
@RequestMapping("/chatRoom")
public class PersonController {

    @Autowired
    PersonServcie personServcie;

    //获取在线人员信息
    @RequestMapping("/getOnlinePerson")
    public String getOnlinePerson() {

        List<Person> personList = personServcie.getOnlinePerson();

        return JSON.toJSONString(personList);
    }

    //人员登录成功记录登录状态
    @RequestMapping("/LoginOn")
    public String LoginOn(String username, String password) {

        Person person = new Person();
        person.setUsername(username);
        person.setPassword(password);

        Long num = RedisUtil.setPersonOnlineStatus(username);

        if (num == 1) {
            return "success";
        } else {
            return "fail";
        }
    }

    //人员注册
    @RequestMapping("/Register")
    public String Register(String username, String password) {

        Person person = new Person();
        person.setUsername(username);
        person.setPassword(password);

        int num = personServcie.Register(person);

        if (num == 1) {
            return "success";
        } else {
            return "fail";
        }
    }

}
